//
//  ZRTabBar.swift
//  weiboDemo
//
//  Created by 张睿 on 16/7/19.
//  Copyright © 2016年 张睿. All rights reserved.
//

import UIKit

class ZRTabBar: UITabBar {
    
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
        // 添加控件
        addSubview(composeButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算UITabBarButton 宽度
        let w = frame.size.width * 0.2
        var index: CGFloat = 0
        // 遍历子控件
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.origin.x = index * w
                value.frame.size.width = w
                
                index++
                if index == 2 {
                    index++
                }
            }
        }
        
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
    }
    
    
    // MARK: - 监听方法
    
    func buttonClick(){
        closure?()
    }
    
    
    // MARK: - 懒加载控件
    private lazy var composeButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.sizeToFit()
        return btn
    }()
}
